package com.android.danaobank.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.danaobank.R;
import com.blankj.utilcode.util.ConvertUtils;

/**
 * ================================================
 * <p>
 * Created by ligang on 2020/9/19  8:43
 * ================================================
 */
public class ImageClickSpanTextView extends FrameLayout {
    private TextView textView;
    private ImageView imageView;

    private int textColor = 0xff333333;
    private int textSize = 13;
    private int lineSpace = ConvertUtils.dp2px(2);
    private Drawable imageDrawable;

    public ImageClickSpanTextView(Context context) {
        super(context);
        init(context, null);
    }

    public ImageClickSpanTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ImageClickSpanTextView);
            textSize = array.getDimensionPixelSize(R.styleable.ImageClickSpanTextView_textSize, textSize);
            lineSpace = array.getDimensionPixelSize(R.styleable.ImageClickSpanTextView_lineSpacing, lineSpace);
            text = array.getString(R.styleable.ImageClickSpanTextView_text);
            textColor = array.getColor(R.styleable.ImageClickSpanTextView_textColor, textColor);
            imageDrawable = array.getDrawable(R.styleable.ImageClickSpanTextView_image);
            array.recycle();
        }
        textView = new TextView(getContext());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setTextColor(textColor);
        textView.setLineSpacing(lineSpace, 1);
        addView(textView);

        imageView = new ImageView(getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(textSize, textSize);
        params.gravity = Gravity.BOTTOM;
        if (imageDrawable != null) {
            imageView.setImageDrawable(imageDrawable);
        }
        imageView.setOnClickListener(v -> {
            if (imageClickListener != null) {
                imageClickListener.onImageClick(v);
            }
        });
        addView(imageView, params);

        setText(text);
    }

    private String findLastLine() {
        Layout layout = textView.getLayout();
        int lineCount = textView.getLineCount();
        if (lineCount == 0) {
            return "";
        }
        if (lineCount == 1) {
            return textView.getText().toString();
        }
        int start = layout.getLineStart(lineCount - 1);
        String line = textView.getText().toString().substring(start);
        return line;
    }

    private String text;

    public void setText(String text) {
        this.text = text;
        textView.setText(text);
        if (imageDrawable != null) {
            post(() -> {
                FrameLayout.LayoutParams params = (LayoutParams) imageView.getLayoutParams();
                String lineText = findLastLine();
                params.leftMargin = (int) textView.getPaint().measureText(lineText) + ConvertUtils.dp2px(3);
                params.topMargin = 0;
                if (params.leftMargin + params.width > getMeasuredWidth()) {
                    params.leftMargin = ConvertUtils.dp2px(1);
                    params.topMargin = textView.getMeasuredHeight() + ConvertUtils.dp2px(2);
                }
                params.bottomMargin = ConvertUtils.dp2px(2);
                imageView.setLayoutParams(params);
            });
        }
    }

    public String getText() {
        return text;
    }

    private ImageClickListener imageClickListener;

    public void setImageClickListener(ImageClickListener imageClickListener) {
        this.imageClickListener = imageClickListener;
    }

    public interface ImageClickListener {
        void onImageClick(View view);
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textView.setTextColor(textColor);
    }
}
